package com.example.rabbitmq.test.service;

import com.example.rabbitmq.test.processors.ProductProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

@Service
@EnableBinding({ProductProcessor.class})
public class ProductService {

    @Autowired
    ProductProcessor productProcessor;

    public void send(){
        // 使用 定义的接口的方式来发送
        System.out.println("新增产品发送");
        productProcessor.outputProductAdd().send(MessageBuilder.withPayload("添加一个产品").build());
    }

    @StreamListener(ProductProcessor.INPUT_PRODUCT_ADD)
    public void receiveProductAdd(Message<String> message) {
        System.out.println("新增产品监听收到：" + message.getPayload());
    }
}
